import copy

file_orig = file("map.c","r")

funs_sorted=[]
for i,line in enumerate(file_orig):
	if not (line[0] in["\t","}","#"] or line.split()==[] or line.startswith("int main") ):
		funs_sorted.append([line,i])
	if("}"==line[0]):
		if(len(funs_sorted[-1])<=2):
			funs_sorted[-1].append(i)

funs_unsorted= copy.deepcopy(funs_sorted)
funs_sorted.sort()
for pair in zip( funs_unsorted, funs_sorted):
	print pair

file_orig = file("map.c","r").readlines()
file_new = file("map.c.sorted","w")

#the begining
for line_num in range(funs_unsorted[0][1]):
	file_new.write(file_orig[line_num])

#the middle
for i,fun in enumerate(funs_sorted):
	for line_num in range(fun[1],fun[2]+1):
		file_new.write(file_orig[line_num])
	if i < len(funs_sorted)-1:
		file_new.write("\n")

#the end
for line_num in range(funs_unsorted[-1][2]+1,len(file_orig)):
	file_new.write(file_orig[line_num])
